//
//  BBLinkTableViewCell.swift
//  BBLinkiOSProject
//
//  Created by liuzhu on 2018/12/7.
//  Copyright © 2018 liuzhu. All rights reserved.
//

import UIKit
import SDWebImage
class BBLinkTableViewCell: UITableViewCell {

    var link:BBLink?{
        didSet{
            nameLabel.text = link?.userLink?.userName
            contentLabel.text = link?.descriptionOC
            timeLabel.text = BBUtils.timeStampToString(timeStamp: link?.updateTime as! Int)
            if link?.linkImages != nil && (link?.linkImages?.hasPrefix("http:"))!{
                let url = URL.init(string: (link?.linkImages)!)
                let imageView = UIImageView()
                imagesView.sd_setImage(with: url, placeholderImage: UIImage(named: "default_image"), options:SDWebImageOptions.init(rawValue: 3), completed: nil)
            }
            else{
                imagesView.removeFromSuperview()
                let width = UIScreen.main.bounds.size.width
                linkFooterView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width:width-20, height:40 ), offset: CGPoint(x: -10, y:10))
                linkFooterView.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: -10, y: 0))
            }
            
            let strHead = link?.userLink?.headImage
            let urlHead = URL.init(string: strHead ?? " ")
            if link?.linkType == 2{
                let distanceK = Int(truncating: (link?.mileage ?? 0))/1000
                let distanceM = (Int(truncating: (link?.mileage ?? 0)) % 1000)/100
                linkFooterView.distanceLabel.text = "\(distanceK).\(distanceM)km"
                linkFooterView.usedTimeLabel.text = "\(Int(truncating: link?.usedTime ?? 0)/3600)h,\(Int(link?.usedTime ?? 0)/60)m,\(Int(truncating: link?.usedTime ?? 0)%60)s"
                linkFooterView.TimesLabel.text = "\(Int(truncating: link?.times ?? 0))times"
            }else{
                linkFooterView.distanceLabel.text = ""
                linkFooterView.usedTimeLabel.text = ""
                linkFooterView.TimesLabel.text = ""
            }
            headImageView.sd_setImage(with: urlHead, placeholderImage: UIImage(named: "default_head"), options:SDWebImageOptions.init(rawValue: 1), completed: nil)
        }
    }
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        //必须加入的方法
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
         contentView.addSubview(headImageView)
         contentView.addSubview(nameLabel)
         contentView.addSubview(timeLabel)
         contentView.addSubview(contentLabel)
         contentView.addSubview(imagesView)
         contentView.addSubview(linkFooterView)
         headImageView.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: contentView, size: CGSize(width: 50, height: 50), offset: CGPoint(x: 10, y: 10))
         nameLabel.xmg_AlignHorizontal(type: XMG_AlignType.TopRight, referView: headImageView, size: nil, offset: CGPoint(x: 10, y: 0))
         timeLabel.xmg_AlignHorizontal(type: XMG_AlignType.BottomRight, referView: headImageView, size: nil, offset: CGPoint(x: 10, y: 0))
         contentLabel.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: headImageView, size: nil, offset: CGPoint(x: 60, y: 20))
        
         let width = UIScreen.main.bounds.size.width
         imagesView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: contentLabel, size: CGSize(width:width-80, height:width/2 ), offset: CGPoint(x: 0, y: 5))
         linkFooterView.xmg_AlignVertical(type: XMG_AlignType.BottomLeft, referView: imagesView, size: CGSize(width:width-80, height:40 ), offset: CGPoint(x: 0, y:10))
         linkFooterView.xmg_AlignInner(type: XMG_AlignType.BottomRight, referView: contentView, size: nil, offset: CGPoint(x: 0, y: 0))
    }
    
    private lazy var headImageView:UIImageView = {
        let iv = UIImageView(image:UIImage(named: "default_head"))
        return iv
    }()
    
    private lazy var nameLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 15)
        return iv
    }()
    
    private lazy var timeLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 13)
        iv.text = "刚刚"
        return iv
    }()
    
    private lazy var contentLabel:UILabel = {
        let iv = UILabel()
        iv.textColor = UIColor.darkGray
        iv.font = UIFont.systemFont(ofSize: 14)
        iv.numberOfLines = 0    //多行
        iv.preferredMaxLayoutWidth = UIScreen.main.bounds.width - 80 //最大宽度
        return iv
    }()
    
//    private lazy var imagesView:LinkImages = {
//        let iv = LinkImages()
//       // iv.backgroundColor = UIColor(red: 249.0/255.0, green: 245.0/255.0, blue: 237.0/255.0, alpha: 1)
//        return iv
//    }()
    private lazy var imagesView:UIImageView = {
        let iv = UIImageView(image:UIImage(named: "default_head"))
          iv.contentMode = UIView.ContentMode.scaleAspectFit
        return iv
    }()
    
    private lazy var linkFooterView:LinkFooterView = {
        let iv = LinkFooterView()
        return iv
    }()
}

class LinkImages: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(linkImage)
        let width = UIScreen.main.bounds.size.width
        linkImage.xmg_AlignInner(type: XMG_AlignType.TopLeft, referView: self, size: CGSize(width:width-80, height:width/2 ), offset: CGPoint(x: 0, y: 0))
    }

    public lazy var linkImage:UIImageView = {
        let iv = UIImageView()
        iv.contentMode = UIView.ContentMode.scaleAspectFit
        iv.clipsToBounds = true
        return iv
    }()
}

class LinkFooterView: UIView {
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI(){
        addSubview(distanceLabel)
        addSubview(usedTimeLabel)
        addSubview(TimesLabel)
        addSubview(likeBtn)
        xmg_HorizontalTile(views: [distanceLabel,usedTimeLabel,TimesLabel,likeBtn], insets: UIEdgeInsets(top: 0, left: 0, bottom: 0, right: 0))
    }
    
    public lazy var distanceLabel:UILabel = {
        let la = UILabel()
        la.textColor = UIColor.lightGray
        la.textAlignment = NSTextAlignment.center
        la.font = UIFont.systemFont(ofSize: 12)
        return la
    }()
    
    public lazy var usedTimeLabel:UILabel = {
        let la = UILabel()
        la.textColor = UIColor.lightGray
        la.textAlignment = NSTextAlignment.center
        la.font = UIFont.systemFont(ofSize: 12)
        return la
    }()
    
    public lazy var TimesLabel:UILabel = {
        let la = UILabel()
        la.textColor = UIColor.lightGray
        la.textAlignment = NSTextAlignment.center
        la.font = UIFont.systemFont(ofSize: 12)
        return la
    }()
    
    private lazy var likeBtn:UIButton = {
        let btn = UIButton()
        btn.setImage(UIImage(named: "timeline_icon_unlike"), for: UIControl.State.normal)
        btn.setTitleColor(UIColor.lightGray, for: UIControl.State.normal)
        btn.titleLabel?.font = UIFont.systemFont(ofSize: 13)
        btn.setTitle("0", for: UIControl.State.normal)
        btn.isHidden = true
        return btn
    }()
}

